package com.baichang.taoshunda.controller.mobile.business;

import com.baichang.framework.controller.BaseMobileController;
import com.baichang.framework.controller.IService;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.controller.ControllerConstants;
import com.baichang.taoshunda.service.OrderService;
import com.baichang.taoshunda.service.ReminderService;
import com.baichang.taoshunda.service.ReservationOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping(ControllerConstants.BUSSINESSORDER)
public class BussinessOrderMController extends BaseMobileController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private ReminderService reminderService;
    @Autowired
    private ReservationOrderService reservationOrderService;

    /**
     * 根据状态获取订单列表
     *
     * @param request
     * @param response
     */
    @RequestMapping("/findBusinessOrderByState")
    public void findBusinessOrderByState(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer bussId = getInt(json, "bussId", 0);
                Integer orderState = getInt(json, "orderState", 0);
                String startDate = getString(json, "startDate", "");
                String endDate = getString(json, "endDate", "");
                Integer nowPage = getInt(json, "nowPage", 1);
                return orderService.findBusinessOrderByState(bussId, orderState, startDate, endDate, nowPage, 20);
            }
        });
    }

    /**
     * 根据状态获取订单列表
     *
     * @param request
     * @param response
     */
    @RequestMapping("/findBusinessHomeOrderByState")
    public void findBusinessHomeOrderByState(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer bussId = getInt(json, "bussId", 0);
                Integer nowPage = getInt(json, "nowPage", 1);
                Integer orderState = getInt(json, "orderState", 0);
                String startDate = getString(json, "startDate", "");
                String endDate = getString(json, "endDate", "");
                return orderService.findBusinessHomeOrderByState(bussId, orderState, startDate, endDate, nowPage, 20);
            }
        });
    }

    /**
     * 商家拒绝接单
     *
     * @param request
     * @param response
     */
    @RequestMapping("/businessRefuseTaking")
    public void businessRefuseTaking(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String orderNumber = getString(json, "orderNumber", "");
                String singleReason = getString(json, "singleReason", "");
                return orderService.businessRefuseTaking(orderNumber, singleReason);
            }
        });
    }

    /**
     * 商家接单
     *
     * @param request
     * @param response
     */
    @RequestMapping("/businessTaking")
    public void businessTaking(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String orderNumber = getString(json, "orderNumber", "");
                return orderService.businessTaking(orderNumber);
            }
        });
    }

    /**
     * 商家同意退款
     *
     * @param request
     * @param response
     */
    @RequestMapping("/businessAgreeBack")
    public void businessAgreeBack(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String orderNumber = getString(json, "orderNumber", "");
                return orderService.businessAgreeBack(orderNumber);
            }
        });
    }

    /**
     * 商家拒绝退款
     *
     * @param request
     * @param response
     */
    @RequestMapping("/businessRefuseBack")
    public void businessRefuseBack(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String orderNumber = getString(json, "orderNumber", "");
                String reason = getString(json, "reason", "");
                return orderService.businessRefuseBack(orderNumber, reason);
            }
        });
    }

    /**
     * 查看订单详情
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getOrderDetail")
    public void getOrderDetail(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String orderNumber = getString(json, "orderNumber", "");
                return orderService.getOrderDetail(orderNumber);
            }
        });
    }

    /**
     * 商家被催单查看
     *
     * @param request
     * @param response
     */
    @RequestMapping("/remindLook")
    public void remindLook(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer reminderId = getInt(json, "reminderId", 0);
                return reminderService.updateRemindLook(reminderId);
            }
        });
    }

    /**
     * 查看订单状态列表
     *
     * @param request
     * @param response
     */
    @RequestMapping("/findOrderStateDetail")
    public void findOrderStateDetail(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String orderNumber = getString(json, "orderNumber", "");
                return orderService.getOrderStateDetail(orderNumber);
            }
        });
    }

    /**
     * 商家同意退货
     *
     * @param request
     * @param response
     */
    @RequestMapping("/agreeBackGoodsOrder")
    public void agreeBackGoodsOrder(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer orderId = getInt(json, "orderId", 0);
                Integer orderDetailsId = getInt(json, "orderDetailId", 0);
                return orderService.agreeBackGoodsOrder(orderId, orderDetailsId);
            }
        });
    }

    /**
     * 商家同意换货
     *
     * @param request
     * @param response
     */
    @RequestMapping("/agreeBarter")
    public void agreeBarter(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer orderId = getInt(json, "orderId", 0);
                Integer orderDetailsId = getInt(json, "orderDetailId", 0);
                return orderService.agreeBarter(orderId, orderDetailsId);
            }
        });
    }

    /**
     * 商家拒绝退货
     *
     * @param request
     * @param response
     */
    @RequestMapping("/refuseBackGoodsOrder")
    public void refuseBackGoodsOrder(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer orderId = getInt(json, "orderId", 0);
                Integer orderDetailsId = getInt(json, "orderDetailId", 0);
                return orderService.refuseBackGoodsOrder(orderId, orderDetailsId);
            }
        });
    }

    /**
     * 商家拒绝换货
     *
     * @param request
     * @param response
     */
    @RequestMapping("/refuseBarter")
    public void refuseBarter(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer orderId = getInt(json, "orderId", 0);
                Integer orderDetailsId = getInt(json, "orderDetailId", 0);
                return orderService.refuseBarter(orderId, orderDetailsId);
            }
        });
    }

    /**
     * 确认退还货物
     *
     * @param request
     * @param response
     */
    @RequestMapping("/sureRetunGoods")
    public void sureRetunGoods(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer orderId = getInt(json, "orderId", 0);
                Integer orderDetailsId = getInt(json, "orderDetailsId", 0);
                return orderService.sureRetunGoods(orderId, orderDetailsId);
            }
        });
    }

    /**
     * 修改未付款的订单金额
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateOrderMoney")
    public void updateOrderMoney(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String orderNumber = getString(json, "orderNumber", "");
                Double money = getDouble(json, "money", 0);
                return orderService.updateOrderMoney(orderNumber, money);
            }
        });
    }

    /**
     * 商家确认消费订单
     */
    @RequestMapping("/companyConsumeOrder")
    public void userConsumeOrder(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                String orderNumber = getString(json, "orderNumber", "");
                Integer id = getInteger(json, "id", 0);
                return reservationOrderService.companyConsumeOrder(orderNumber, id);
            }
        });
    }

    /**
     * 已送达
     *
     * @param request
     * @param response
     */
    @RequestMapping("/deliveredOrder")
    public void deliveredOrder(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String orderNumber = getString(json, "orderNumber", "");
                return orderService.deliveredOrderByCompany(orderNumber);
            }
        });
    }

    /**
     * 订单详情查询
     */
    @RequestMapping("/getReservationOrderDetails")
    public void getReservationOrderDetails(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
//                Integer orderId = getInt(json, "orderId", 0);
                String orderNumber = getString(json, "orderNumber", "");
                return orderService.getReservationOrderDetails(orderNumber);
            }
        });
    }

    /**
     * 订单列表查询
     */
    @RequestMapping("/getReservationOrderList")
    public void getReservationOrderList(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer bussId = getInt(json, "bussId", 0);
                Integer orderState = getInt(json, "orderState", 0);
                Integer nowPage = getInt(json, "nowPage", 1);
                Integer pageSize = getInt(json, "pageSize", 20);
                String startDate = getString(json, "startDate", "");
                String endDate = getString(json, "endDate", "");
                return orderService.getReservationOrderListByCompanyId(orderState, bussId, nowPage, pageSize, startDate, endDate);
            }
        });
    }

    /**
     * 用户端删除订单
     *
     * @param request
     * @param response
     */
    @RequestMapping("/removeOrderCompanyStatus")
    public void removeOrderUserStatus(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String orderNumber = getString(json, "orderNumber", "");

                return orderService.removeOrderCompanyStatus(orderNumber);
            }
        });
    }

    /**
     * 查看订单和互动的未读总数
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getTrendCountAndOrderCount")
    public void getTrendCountAndOrderCount(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer userId = getInt(json, "userId", 0);
                Integer trendsId = getInt(json, "trendsId", 0);
                return orderService.getTrendCountAndOrderCount(userId, trendsId);
            }
        });
    }

    /**
     * 查看订单商品的退换货理由
     *
     * @param request
     * @param response
     */
    @RequestMapping("/findReasonByDetailsId")
    public void findReasonByDetailsId(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer orderDetailsId = getInt(json, "orderDetailId", 0);
                return orderService.findReasonByDetailsId(orderDetailsId);
            }
        });
    }

    /**
     * 功能描述:
     *
     * @param: 商家同意退款之后，确认归还货物
     * @return:
     * @auther: wsl
     * @date: 2018/11/22 10:41
     */
    @RequestMapping("/companySure")
    public void companySure(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer orderId = getInt(json, "orderId", 0);
                Integer companyIsSure = getInt(json, "companyIsSure", 1);
                return orderService.companySure(orderId, companyIsSure);
            }
        });
    }

    /**
     * 功能描述:
     *
     * @param: 商家同意退款之后，确认归还货物
     * @return:
     * @auther: wsl
     * @date: 2018/11/22 10:41
     */
    @RequestMapping("/js")
    public void js(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer orderId = getInt(json, "orderId", 0);

                return orderService.js(orderId);
            }
        });
    }
}
